package com.zxbg.controller.systemtoolsController;

import com.zxbg.controller.Code;
import com.zxbg.controller.Result;
import com.zxbg.domain.InfoBlock;
import com.zxbg.service.systemtoolsService.InfoBlockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/InfoBlock")
public class InfoBlockController {
    @Autowired
    InfoBlockService infoBlockService;

    @PostMapping("/{info_module_id}")
    public Result getListByModuleId(@PathVariable String info_module_id, @RequestHeader Integer tenant_id, @RequestBody Map<String, Object> search_info, @RequestHeader String employee_id) {
        List<InfoBlock> infoBlockList = infoBlockService.getListByModuleId(info_module_id, tenant_id, search_info, employee_id);
        List<InfoBlock> resList = new ArrayList<>();
        Integer startNum = ((Integer)search_info.get("current_page") - 1) * (Integer)search_info.get("page_size");
        Integer endNum = (Integer)search_info.get("current_page") * (Integer)search_info.get("page_size") - 1;
        for (int i = 0; i < infoBlockList.size(); i++) {
            if(i >= startNum && i <= endNum) {
                resList.add(infoBlockList.get(i));
            }
        }
        Map<String,Object> first_map = new HashMap<>();
        Map<String,Object> second_map = new HashMap<>();
        second_map.put("page_size", search_info.get("page_size"));
        second_map.put("current_page", search_info.get("current_page"));
        second_map.put("total", infoBlockList.size());
        first_map.put("page", second_map);
        first_map.put("rows", resList);
        return new Result(Code.GET_OK, first_map);
    }

    @PostMapping
    public Result addInfoBlock(@RequestBody InfoBlock infoBlock, @RequestHeader String employee_id, @RequestHeader Integer tenant_id) {
        infoBlock.setCreator_id(employee_id);
        infoBlock.setTenant_id(tenant_id);
        boolean flag = infoBlockService.addInfoBlock(infoBlock);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @PutMapping("/stop")
    public Result stopInfoBlock(@RequestBody Integer[] ids) {
        boolean flag = infoBlockService.stopInfoBlock(ids);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag, flag ? "success" : "操作失败");
    }

    @PutMapping("/changeIsTop")
    public Result changeIsTop(@RequestBody Map<String, Integer> map) {
        boolean flag = infoBlockService.changeIsTop(map.get("id"), map.get("is_top"));
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag, flag ? "success" : "操作失败");
    }

    @DeleteMapping("/{id}")
    public Result deleteInfoBlock(@PathVariable Integer id) {
        return new Result(Code.DELETE_OK, infoBlockService.deleteInfoBlock(id));
    }

    @PutMapping("/publishInfoBlock/{id}")
    public Result publishInfoBlock(@PathVariable Integer id) {
        boolean flag = infoBlockService.publishInfoBlock(id);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag, flag ? "success" : "操作失败");
    }

    @PutMapping("/useRightNow")
    public Result useRightNow(@RequestBody Integer[] ids) {
        boolean flag = infoBlockService.useRightNow(ids);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag, flag ? "success" : "操作失败");
    }

    @PutMapping
    public Result updateInfoBlock(@RequestBody InfoBlock infoBlock) {
        return new Result(Code.UPDATE_OK, infoBlockService.updateInfoBlock(infoBlock));
    }
}
